package com.yfree.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

import com.yfree.interfaces.YOnScrollViewListener;

public class YListenerScrollView extends ScrollView {
    public boolean canDoScrollBottom = true;
    private YOnScrollViewListener scrollViewListener = null;
    private YPullToRefreshScrollView.OnScrolledToBottom onScrolledToBottom;

    public void setOnScrolledToBottom(YPullToRefreshScrollView.OnScrolledToBottom onScrolledToBottom) {
        this.onScrolledToBottom = onScrolledToBottom;
    }

    public interface OnScrolledToBottom {
        void onScrolledToBottom();
    }

    public YListenerScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public YListenerScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public YListenerScrollView(Context context) {
        super(context);
    }

    public void setOnScrollViewListener(YOnScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(x, y, oldx, oldy);
        }
    }

    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (canDoScrollBottom && scrollY != 0 && clampedY && onScrolledToBottom != null) {
            canDoScrollBottom = false;
            onScrolledToBottom.onScrolledToBottom();
        }
    }
}
